package com.caigua.mgp.mg.codegen.customize.xmlmapper.elements;

import com.caigua.mgp.mg.codegen.customize.CustomizeMyBatis3FormattingUtilities;
import com.caigua.mgp.mg.internal.CustomizeIntrospectedTableMyBatis3Impl;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;

/**
 * @BelongsProject: mybatis_generator_pluing
 * @BelongsPackage: com.caigua.mgp.mg.codegen.customize
 * @Author: caigua
 * @CreateTime: 2020-12-09 14:30
 * @Description: todo
 */
public abstract class CustomizeAbstractXmlElementGenerator extends AbstractXmlElementGenerator {

    protected XmlElement getListPageFilterSqlElement() {
        StringBuilder sb = new StringBuilder();
        XmlElement filterSql = new XmlElement("sql");
        filterSql.addAttribute(new Attribute("id", ((CustomizeIntrospectedTableMyBatis3Impl)this.introspectedTable).getListPageFilterId()));
        filterSql.addElement(new TextElement("1 = 1"));

        for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
            FullyQualifiedJavaType columnFullyQualifiedJavaType = introspectedColumn.getFullyQualifiedJavaType();
            if(!"java.util.Date".equalsIgnoreCase(columnFullyQualifiedJavaType.getFullyQualifiedName())){
                sb.setLength(0);
                sb.append(introspectedColumn.getJavaProperty("filtter."));
                sb.append(" != null"); //$NON-NLS-1$

                if("java.lang.String".equalsIgnoreCase(columnFullyQualifiedJavaType.getFullyQualifiedName())) {
                    sb.append(" and ");
                    sb.append(introspectedColumn.getJavaProperty("filtter."));
                    sb.append(" != ''");
                }

                XmlElement anIf = new XmlElement("if");
                anIf.addAttribute(new Attribute("test", sb.toString()));

                sb.setLength(0);
                sb.append(" and ");
                sb.append(MyBatis3FormattingUtilities
                        .getEscapedColumnName(introspectedColumn));
                sb.append(" = "); //$NON-NLS-1$
                sb.append(MyBatis3FormattingUtilities
                        .getParameterClause(introspectedColumn, "filtter."));
                anIf.addElement(new TextElement(sb.toString()));
                filterSql.addElement(anIf);
            }else{
                filterSql.addElement(getTimeIntervalQuerySql(introspectedColumn, "fillter.start", ">="));
                filterSql.addElement(getTimeIntervalQuerySql(introspectedColumn, "fillter.end", "<="));
            }

        }
        return filterSql;
    }

    protected XmlElement getTimeIntervalQuerySql(IntrospectedColumn introspectedColumn, String prefix, String mathematicsSymbol) {
        StringBuilder sb = new StringBuilder();
        sb.setLength(0);
        sb.append(CustomizeMyBatis3FormattingUtilities.getJavaPropertyToCamelCase(introspectedColumn, prefix));
        sb.append(" != null"); //$NON-NLS-1$
        XmlElement anIf = new XmlElement("if");
        anIf.addAttribute(new Attribute("test", sb.toString()));

        sb.setLength(0);
        sb.append(" and ");
        sb.append("<![CDATA[ DATE_FORMAT(");
        sb.append(MyBatis3FormattingUtilities
                .getEscapedColumnName(introspectedColumn));
        sb.append(",'%Y-%m-%d %H:%i:%s')");
        sb.append(" " + mathematicsSymbol + " "); //$NON-NLS-1$
        sb.append("DATE_FORMAT(");
        sb.append(CustomizeMyBatis3FormattingUtilities
                .getParameterClausetoCamelCase(introspectedColumn, prefix));
        sb.append(",'%Y-%m-%d 00:00:00') ]]>");
        anIf.addElement(new TextElement(sb.toString()));
        return anIf;
    }

    protected XmlElement getListPageSortSqlElement() {
        StringBuilder sb = new StringBuilder();
        XmlElement sortSql = new XmlElement("sql");
        sortSql.addAttribute(new Attribute("id", ((CustomizeIntrospectedTableMyBatis3Impl)this.introspectedTable).getListPageSortId()));
        XmlElement chooseElement = new XmlElement("choose");
        XmlElement whenElement = new XmlElement("when");
        whenElement.addAttribute(new Attribute("test", "sort == null or sort.isEmpty()"));
        whenElement.addElement(new TextElement("ORDER BY create_time DESC"));
        XmlElement otherwiseElement = new XmlElement("otherwise");
        XmlElement trimElement = new XmlElement("trim");
        trimElement.addAttribute(new Attribute("prefix", "ORDER BY"));
        trimElement.addAttribute(new Attribute("suffixOverrides", ","));
        for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
            XmlElement ifElement2 = new XmlElement("if");
            sb.setLength(0);
            sb.append(introspectedColumn.getJavaProperty("sort."));
            sb.append(" != null");
            sb.append(" and ");
            sb.append("(");
            sb.append(introspectedColumn.getJavaProperty("sort."));
            sb.append(" == 'desc'");
            sb.append(" or ");
            sb.append(introspectedColumn.getJavaProperty("sort."));
            sb.append(" == 'asc'");
            sb.append(")");
            ifElement2.addAttribute(new Attribute("test", sb.toString()));

            sb.setLength(0);
            sb.append(MyBatis3FormattingUtilities
                    .getEscapedColumnName(introspectedColumn));
            sb.append(" ${");
            sb.append(introspectedColumn.getJavaProperty("sort."));
            sb.append("},");
            ifElement2.addElement(new TextElement(sb.toString()));
            trimElement.addElement(ifElement2);
        }


        otherwiseElement.addElement(trimElement);
        chooseElement.addElement(whenElement);
        chooseElement.addElement(otherwiseElement);
        sortSql.addElement(chooseElement);
        return sortSql;
    }

    protected XmlElement getListPageFilterElement() {
        XmlElement whereElement = new XmlElement("where");
        XmlElement includeElement = new XmlElement("include");
        includeElement.addAttribute(new Attribute("refid", ((CustomizeIntrospectedTableMyBatis3Impl)this.introspectedTable).getListPageFilterId()));
        whereElement.addElement(includeElement);
        return whereElement;
    }

    protected XmlElement getListPageSortElement() {
        XmlElement includeElement = new XmlElement("include");
        includeElement.addAttribute(new Attribute("refid", ((CustomizeIntrospectedTableMyBatis3Impl)this.introspectedTable).getListPageSortId()));
        return includeElement;
    }
}
